/**
 * [getPathById 根据数字id生成文件路径，补齐至9位后每3位数字一级目录]
 * @param  {[Number|String]} str [传入的id的字符串或者数字]
 * @param  {[String]} sep [传入的id]
 * @return {[String]}    [例如：id为12345，则文件路径为000/012/345]
 */
export function getPathById(str = '', sep = '/') {
    if (typeof str !== 'string' || Number(str) === 0) {
        if (typeof str === 'number' && str !== 0) {
            str = str.toString();
        } else {
            return `000${sep}000${sep}000`;
        }
    }

    let rs,
        row, col, i, j,
        len = str.length;

    for (i = 9 - len; i > 0; i--) {
        str = '0' + str;
    }
    len = str.length;
    row = Math.floor(len / 3);
    col = len % 3;
    rs = str.substring(0, col);
    for (i = 0, j = col; i < row; i++, j = 3 * i + col) {
        rs += (i === 0 && col === 0 ? '' : sep) + str.substring(j, j + 3);
    }

    return rs;
}

//原api的实现方式
export function _getPathById(id) { 
    id = (id || 0).toString();
    if (id.length < 9) {
        id = ('000000000' + id).slice(-9);
    }
    let result = '';
    const len = id.length;
    const max = Math.ceil(len / 3);
    let i = 0;

    while (i < max) {
        result = '/' + id.slice(-3 * i - 3, len - 3 * i) + result;
        i++;
    }

    return result.substring(1);
}
    

